package com.pzh.guli.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzh.guli.service.base.result.Result;
import com.pzh.guli.service.edu.pojo.Course;
import com.pzh.guli.service.edu.pojo.Teacher;
import com.pzh.guli.service.edu.pojo.vo.*;
import com.pzh.guli.service.edu.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xzh
 * @since 2022-05-11
 */
@RestController
@RequestMapping("/admin/edu/course")
public class CourseController {
    @Autowired
    private ICourseService courseService;
   @PostMapping("save-course-info")
   public Result saveCourseInfo(@RequestBody CourseVo courseVo){
       String courseId= courseService.saveCourse(courseVo);
       return Result.ok().message("保存成功").data("courseID",courseId);
   }
   @GetMapping("course-info/{id}")
   public Result getCourseById(@PathVariable String id){
       CourseVo courseVo=  courseService.getCourseInfoById(id);
       return Result.ok().data("items",courseVo);
   }
    @PutMapping("update-course-info")
    public Result updateCourseInfo(@RequestBody CourseVo courseVo){
        String courseId= courseService.updateCourse(courseVo);
        return Result.ok().message("更新成功").data("courseID",courseId);
    }
    @GetMapping("list/{page}/{limit}")
    public Result listPage(@PathVariable Long page, @PathVariable Long limit, CourseQueryVo courseQueryVo){
        Page<CourseDto> iPage=courseService.selectPage(page,limit,courseQueryVo);
        List<CourseDto> records=iPage.getRecords();
        long total = iPage.getTotal();
        return Result.ok().data("total",total).data("rows",records);
    }
    //发布课程 获得发布的信息
    @GetMapping("/publish/{id}")
    public Result publish(@PathVariable String id){
       CoursePublishVo publishVo = courseService.getPublishCourseById(id);
       return Result.ok().data("item",publishVo);
    }
    //发布课程 更改状态
    @PostMapping("/doPublish/{id}")
    public Result doPublish(@PathVariable String id){
       boolean flag= courseService.updatePublish(id);
       if (flag){
           return Result.ok().message("课程发布成功");
       }
       return Result.error().message("课程发布失败");
    }
}

